﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading.Tasks;

namespace FDSimpleModelGenerator.IServices
{
    /// <summary>
    /// 数据库访问接口
    /// </summary>
    public interface IDbAccessor
    {
        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="dbConnection">创建数据链接</param>
        Task InitAsync(Func<IDbConnection> dbConnection);

        /// <summary>
        /// 获取单个
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sql"></param>
        /// <param name="parameter"></param>
        /// <returns></returns>
        Task<T> GetAsync<T>(string sql, object parameter = null);

        /// <summary>
        /// 获取对象列表
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sql"></param>
        /// <param name="parameter"></param>
        /// <returns></returns>
        Task<IList<T>> GetListAsync<T>(string sql, object parameter = null);

        /// <summary>
        /// 执行
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="parameter"></param>
        /// <param name="isTransaction"></param>
        /// <returns></returns>
        Task<int> ExecuteNoneQueryAsync(string sql, object parameter = null, bool isTransaction = false);

        /// <summary>
        /// 执行有返回
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="parameter"></param>
        /// <param name="isTransaction"></param>
        /// <returns></returns>
        Task<T> ExecuteScalarAsync<T>(string sql, object parameter = null, bool isTransaction = false);

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="dbConnection">创建数据链接</param>
        void Init(Func<IDbConnection> dbConnection);

        /// <summary>
        /// 获取单个
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sql"></param>
        /// <param name="parameter"></param>
        /// <returns></returns>
        T Get<T>(string sql, object parameter = null);

        /// <summary>
        /// 获取对象列表
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sql"></param>
        /// <param name="parameter"></param>
        /// <returns></returns>
        IList<T> GetList<T>(string sql, object parameter = null);

        /// <summary>
        /// 执行
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="parameter"></param>
        /// <param name="isTransaction"></param>
        /// <returns></returns>
        int ExecuteNoneQuery(string sql, object parameter = null, bool isTransaction = false);

        /// <summary>
        /// 执行有返回
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="parameter"></param>
        /// <param name="isTransaction"></param>
        /// <returns></returns>
        T ExecuteScalar<T>(string sql, object parameter = null, bool isTransaction = false);

        /// <summary>
        /// 开始事务
        /// </summary>
        /// <returns></returns>
        Task<IDbConnection> BeginTransaction();

        /// <summary>
        /// 提交事务
        /// </summary>
        /// <returns></returns>
        Task<bool> CommitTransaction();

        /// <summary>
        /// 回滚事务
        /// </summary>
        /// <returns></returns>
        Task<bool> RollbackTransaction();
    }
}
